<?php

/**
 * 马拉松
 */
class marathonModel
{


    public function __construct(& $base)
    {
        $this->base = $base;
        $this->db = & $this->base->db;
        $this->redis = & $this->base->redis;
    }
    
    /**
     * 获取app活动入口显示条目
     * 
     * @return array 返回结果数组
     */
    public function getDisplay() {
        $cacheKey = REDIS_PREFIX . 'Marathon_display';
        $retDatas = [];

        if (!$this->redis->exists($cacheKey)) {
            $rowData  = $this->db->getRowTable('r_activity_marathon', ['id', 'status', 'start_time', 'end_time'], 'display=1');
            $rowData['url'] = "http://" . $this->base->config['host'] . '/marathonWeb/marathonIndex/?m_id=' . $rowData['id'];
            $retDatas = $rowData;
            $this->redis->set($cacheKey, json_encode($rowData));
            $this->redis->expire($cacheKey, $rowData['end_time'] - time());
        } else {
            $retDatas = json_decode($this->redis->get($cacheKey), true);
        }
        
        return $retDatas;
    }
    
    /**
     * 获取马拉松活动详情
     * @param numeric $marathonId
     * @param array $field
     * @return array  马拉松信息数组
     */
    public function getMarathonInfo($marathonId, $fields = []){
        if (empty($marathonId) || !is_numeric($marathonId)) {
            return [];
        }
        
        return $this->db->getRowTable('r_activity_marathon', $fields, 'id=' . $marathonId);
    }
}